/*
 * Copyright (c) 2022.版权所有高金磊
 */

package 领扣算法.AA中等题.统计隐藏数组数目;

public class Main {
    public int numberOfArrays(int[] differences, int lower, int upper) {
        int min=0,max=0;
        int middle=0;
        for (int i = 0; i < differences.length; i++) {
            differences[i]+=middle;
            middle=differences[i];
            min=Math.min(differences[i],min);
            max=Math.max(differences[i],max);
            if (upper-max-lower+min+1<=0)
                return 0;
        }
        min=Math.min(min, 0);
        max=Math.max(max, 0);
        lower-=min;
        upper-=max;
        lower=upper-lower+1;
        return lower<0?0:lower;
    }
}
